<?php

namespace app\index\controller;

use think\Controller;
use think\Request;

class Order extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function confirm()
    {
        if (!$this->getLoginUser()) {
            $this->error('请登录', url('user/login'));
        }
        //获取参数
        $id = input('get.id', 0, 'intval');
        if (!$id) {
            $this->error('参数错误');
        }
        $num = input('get.count', 1, 'intval');
        $deal = model('deal')->get($id);

        if (!$deal || $deal->status != 1) {
            $this->error('商品不存在');
        }
        return $this->fetch('', ['controller' => 'pay', 'title' => '支付页面', 'deal' => $deal, 'user' => $this->account, 'count' => $num]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function index()
    {
        //获取参数信息
        //判断用户是否登录
        if (!$this->getLoginUser()) {
            $this->error('请登录', url('user/login'));
        }
        $data = input('get.');
        $validate = validate('Order');
        if (!$validate->scene('index')->check($data)) {
            $this->error($validate->getError());
        }
        if (empty($_SERVER['HTTP_REFERER'])) {
            $this->error('请求不合法');
        }
        //组装入库数据
        $order_sn = setOrderSn();
        $data = [
            'out_trade_no' => $order_sn,
            'user_id' => $this->getLoginUser()->id,
            'user_name' => $this->getLoginUser()->username,
            'deal_id' => $data['id'],
            'deal_count' => $data['deal_count'],
            'total_price' => $data['total_price'],
            'pay_status' => 0,
            'referer' => $_SERVER['HTTP_REFERER']
        ];
        //验证成功数据入库
        try {
            $orderId = model('order')->add($data);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        if ($pay = input('get.pay') == 1) {
            $this->redirect(url('pay/alipay', ['id' => $orderId]));
        } elseif ($pay = input('get.pay') == 2) {
            $this->redirect(url('pay/wxpayqcode', ['id' => $orderId]));
        }
        $this->redirect(url('pay/index', ['id' => $orderId]));

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
